package com.hugh.systeminfo;

import java.util.Arrays;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemClickListener{
    
    private static final String TAG = "MainActivity";
    /**
     * 功能菜单名称列表
     */
    private static final String[] MENU = new String[]{"Logcat"};
    
    private LayoutInflater mInflater;
    
    private GridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mInflater = LayoutInflater.from(getApplicationContext());
        
        mGridView = (GridView) findViewById(R.id.gv_menu);
        mGridView.setAdapter(new MenuAdapter(Arrays.asList(MENU)));
        mGridView.setOnItemClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
        Log.d(TAG, "onItemClick position:" + position);
        //跟menu顺序一致
        switch(position){
        case 0: //logcat
            Intent itt = new Intent(getApplicationContext(), LogcatActivity.class);
            startActivity(itt);
            break;
        }
    }
    
    private class MenuAdapter extends BaseAdapter{
        
        private List<String> mMenuList;
        
        public MenuAdapter(List<String> menus){
            mMenuList = menus;
        }

        @Override
        public int getCount() {
            if(mMenuList != null){
                return mMenuList.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if(mMenuList != null){
                return mMenuList.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            TextView tv = null;
            if(v == null){
                v = mInflater.inflate(R.layout.item_menu, parent, false);
                tv = (TextView) v.findViewById(R.id.tv_name);
                
                v.setTag(tv);
            } else {
                tv = (TextView) v.getTag();
            }
            
            if(mMenuList != null){
                tv.setText(mMenuList.get(position));
            } else {
                tv.setText("");
            }
            
            return v;
        }
    }
}
